package com.zues.thread.design_mode.guarded_stop;

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

public class MailBoxes {


    private static Map<Integer, GuardedObject> boxes = new Hashtable<>();

    private static int id = 1;

    /**
     * 产生唯一的id
     * @return
     */
    private static synchronized int generaterId(){
        return id++;
    }

    /**
     * 根据ID获取对象
     * @param id
     * @return
     */
    public static GuardedObject getGuardedObject(Integer id){
//        GuardedObject guardedObject = boxes.get(id);
//        if(guardedObject == null){
//            return null;
//        }
//        Object object = guardedObject.get(timeout);

        return boxes.remove(id);
    }

    public static GuardedObject createGuardedObject(){
        int id = generaterId();
        GuardedObject guardedObject = new GuardedObject(id);
        boxes.put(guardedObject.getId(), guardedObject);
        return guardedObject;
    }

    public static Set<Integer> getIds(){
        return boxes.keySet();
    }

}
